<?php

/**
 * CodeIgniter Captcha Class
 *
 * 园所验证类
 */
class CT_auth_nursery
{
    /**
     * 设置用户登陆状态
     * 
     * @return array
     */
    public function set_nursery_user_login($nursery)
    {
        $ci = & get_instance();
        $ci->load->library('session');
    
        $info = array(
            'nid' => $nursery['id'],
            'ntitle' => $nursery['title'],
            'nrid' => $nursery['rid'] ?:'',
            'ntoken' => $this->hashSSHA($nursery['uid']),
            'nuid' => $nursery['uid']?:'',
            'nusername' => $nursery['username']?:'',
        );
    
        return $ci->session->set_userdata($info);
    }
    
    /**
     * 取登陆用户信息
     *
     * @return array
     */
    public function get_nursery_user_login()
    {
        $ci = & get_instance();
        $ci->load->library('session');
        return $ci->session->all_userdata();
    }
    
    
    /**
     * 判断用户是否登陆
     * @return array
     */
    public function is_nursery_login()
    {
        $ci = & get_instance();
        $ci->load->library('session');
        $is_login = $ci->session->all_userdata();
        if ($is_login) {
            $token = $ci->session->userdata('ntoken');
            $uid = $ci->session->userdata('nuid');
            if ($this->hashSSHA($uid) == $token) {
                return $this->get_nursery_user_login();
            }
        }
        return array();
    }
    
    /**
     * 用户退出
     * @return  null
     */
    public function set_nursery_user_logout()
    {
        $ci = & get_instance();
        $ci->load->library('session');
        return $ci->session->sess_destroy();
    }
    
    /**
     * 密码加密
     * @param  string $password
     * @return string
     */
    public function hashSSHA($password)
    {
        $salt = substr($password, 0, 4);
        $hash = base64_encode( sha1($password . $salt, true) . $salt );
        return $hash;
    }
}
